package com.gzhryc.shared_device.oem.h5.mng.controllers.market;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.event.DelayHandleService;
import com.gzhryc.net_marketing.cloud.business.dao.db.TeamPayApply;
import com.gzhryc.net_marketing.cloud.business.dao.db.TeamPayApplyMember;
import com.gzhryc.net_marketing.cloud.business.dao.enums.TeamPayApplyEnum;
import com.gzhryc.net_marketing.cloud.business.event.TeamPayApplySettlementEvent;
import com.gzhryc.net_marketing.cloud.business.services.TeamPayApplyMemberService;
import com.gzhryc.net_marketing.cloud.business.services.TeamPayApplyService;
import com.gzhryc.net_marketing.cloud.business.services.dto.TeamPayApplySearch;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

public class TeamPayApplyController {

	@Route(value = { "/business/team_pay/apply/list.html" })
	public String list(HttpServletRequest request) {
		request.setAttribute("stateList", TeamPayApplyEnum.State.values());
		return "/business/team_pay_apply/list.html";
	}

	@Route(value = "/business/team_pay/apply/list.data")
	public ListResult<TeamPayApply> listData(HttpServletRequest request) {
		ListResult<TeamPayApply> result = new ListResult<TeamPayApply>(DtoConstants.SUCCESS);

		TeamPayApplySearch search = WebUtils.getParameter(request, TeamPayApplySearch.class);
		Integer pageNo = WebUtils.getInteger(request, "page", 1);
		Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

		result.getBody().setTotal(TeamPayApplyService.self().countBySearch(search));
		result.getBody().setData(TeamPayApplyService.self().findBySearch(search, pageNo, pageLimit));

		if (result.getBody().getTotal() == 0) {
			result.setCode(DtoConstants.NULL_DATA, "暂无数据");
		}

		return result;
	}
	
	@Route(value = { "/business/team_pay/apply/info.html" })
	public String info(HttpServletRequest request) {
		Long id = WebUtils.getLong(request, "id");
		if(id != null) {
			TeamPayApply teamPayApply = TeamPayApplyService.self().getById(id);
			if(teamPayApply != null) {
				WebUtils.setAttribute(request, teamPayApply);
			}
		}
		return "/business/team_pay_apply/info.html";
	}
	
	@Route(value = "/business/team_pay/apply/member/list.data")
	public ListResult<TeamPayApplyMember> memberListData(HttpServletRequest request) {
		ListResult<TeamPayApplyMember> result = new ListResult<TeamPayApplyMember>(DtoConstants.SUCCESS);
		Long applyId = WebUtils.getLong(request, "applyId");
		if(applyId != null) {
			List<TeamPayApplyMember> memberList = TeamPayApplyMemberService.self().findByApplyId(applyId);
			if(memberList != null && memberList.size() > 0) {
				result.getBody().setData(memberList);
			}else {
				result.setCode(DtoConstants.NULL_DATA, "暂无数据");
			}
		}
		return result;
	}

	@Route(value = { "/business/team_pay/apply/settlement.do" })
	public AjaxResult toSettlement(HttpServletRequest request){
		AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"结算处理失败");
		Long id = WebUtils.getLong(request, "id");
		if(id != null) {
			TeamPayApply apply = TeamPayApplyService.self().getById(id);
			if(apply != null) {
				if(TeamPayApplyEnum.State.WaitSettlement.index().equals(apply.getState())) {
					TeamPayApplySettlementEvent event = new TeamPayApplySettlementEvent(apply);
					DelayHandleService.get().addEvent(event);
					result.setCode(DtoConstants.SUCCESS, null);
				}else{
					result.setMsg("当前状态不容许结算");
				}
			}else{
				result.setMsg("未找到团拼申请");
			}
		}
		return result;
	}

	@Route(value = { "/business/team_pay/apply/del.do" })
	public AjaxResult del(HttpServletRequest request){
		AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"删除失败");
		Long id = WebUtils.getLong(request, "id");
		if(id != null) {
			if(TeamPayApplyService.self().deleteById(id)) {
				result.setCode(DtoConstants.SUCCESS,null);
			}
		}
		return result;
	}
}
